package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annottation.AddCacheAnnotation;
import com.baizhi.cmfz.annottation.DeleteCacheAnnotation;
import com.baizhi.cmfz.dao.ConsumerDao;
import com.baizhi.cmfz.entity.Consumer;
import com.baizhi.cmfz.entity.UserDTO;
import com.baizhi.cmfz.service.ConsumerService;
import com.baizhi.cmfz.util.ThreadPollUtil;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.apache.ibatis.annotations.Delete;
import org.apache.poi.ss.formula.ptg.MemAreaPtg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import static com.sun.tools.corba.se.idl.constExpr.Expression.two;

@Service
@Transactional
public class ConsumerServiceImpl implements ConsumerService {
   private Logger logger= LoggerFactory.getLogger(ConsumerServiceImpl.class);
   @Autowired
    private ConsumerDao consumerDao;
    @Override
    @AddCacheAnnotation
    public Map selectByPageConsumer(Integer page, Integer limit, String name) {
      logger.debug("查询所有的用户信息");
       Map map=new HashMap();
       int offset=(page-1)*limit;
       if(name != null && !name.equals("")){
           name="%"+name+"%";
       }else {
           name=null;
       }
        logger.debug("查询总条数");
        int count = consumerDao.selectTotalCountConsumer(name);
        List<Consumer> consumers = consumerDao.selectByPageConsumer(offset, limit, name);
        map.put("data",consumers);
        map.put("count",count);
        map.put("msg","");
        map.put("code",0);
        logger.debug("执行结束");
        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public Map insertConsumer(Consumer consumer) {
        Map map=new HashMap();
       int count =consumerDao.insertConsumer(consumer);
       if(count>0){
           map.put("isInsert",true);
       }else{
           map.put("isInsert",false);
       }
        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public Map updateConsumer(Consumer consumer) {
      Map map=new HashMap();
        int count = consumerDao.updateConsumer(consumer);
    if(count>0){
        map.put("isUpdate",true);
    }else {
        map.put("isUpdate",false);
    }
        return map;
    }

    @Override
    @AddCacheAnnotation
    public Consumer selectOneConsumer(Integer id) {
        return consumerDao.selectOneConsumer(id);
    }

    @Override
    @DeleteCacheAnnotation
    public Map deleteConsumer(Integer id) {
        Map map=new HashMap();
       int count= consumerDao.deleteConsumer(id);
       if(count>0) {
           map.put("isDelete", true);
       }else {
           map.put("isDelete",false);
       }
        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public Map updateFreeze(Integer id) {
        Map map=new HashMap();
        int count=consumerDao.updateFreeze(id);
        if(count>0) {
            map.put("isUpdate", true);
        }else {
            map.put("isUpdate",false);
        }
        return map;
    }

    @Override
    @AddCacheAnnotation
    public List<Map> selectNumberConsumer() {
        return consumerDao.selectAll();
    }

    @Override
    @AddCacheAnnotation
    public Map selectNumber() {
        Map map=new HashMap();
        List<UserDTO> userDTOS = consumerDao.selectNumber();
        UserDTO userDTO = userDTOS.get(0);
        map.put("userDTOS",userDTO);
        return map;
    }

    @Override
    @AddCacheAnnotation
    public Map selectByDay() {
        Map map=new HashMap();
        int one = consumerDao.selectByDayCount(1, 7);
        int two = consumerDao.selectByDayCount(7, 14);
        int three = consumerDao.selectByDayCount(14, 21);
        map.put("one",one);
        map.put("two",two);
        map.put("three",three);

        return map;
    }

    @Override
    @AddCacheAnnotation
    public Map selectAllOrder() {

        long start = System.currentTimeMillis();
        Map map=new HashMap();
        //查询用户地区分布的人数
        List<Map> maps = selectNumberConsumer();
        //查询男女人数  比例
        Map map1 = selectNumber();
        //查询过去三周的注册量人数
        Map map2 = selectByDay();

        map.put("sex",map1);
        map.put("week",map2);
        map.put("count",maps);
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
        return map;
    }

    @Override
    @AddCacheAnnotation
    public Map selectOrderAll() {
        //获取当前的系统时间 毫秒级别
        //该方法的作用是返回当前的计算机时间，
        //时间的表达格式为当前计算机时间和GMT时间(格林威治时间)
        // 1970年1月1号0时0分0秒所差的毫秒数。
        long start = System.currentTimeMillis();
        Map map=new HashMap();
        //1.创建计数器
        CountDownLatch countDownLatch=new CountDownLatch(3);

        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                Map map1 = selectNumber();
                map.put("sex",map1);
                //计数器减一
                countDownLatch.countDown();
            }
        });


        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> maps = selectNumberConsumer();
                map.put("count",maps);
                //计数器减一
                countDownLatch.countDown();
            }
        });


        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                Map map2 = selectByDay();
                map.put("week",map2);
                //计数器减一
                countDownLatch.countDown();
            }
        });

        //让主线程等待所有的任务线程结束 设置的值减到0时在return
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");

        return map;
    }


}
